﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using System.Threading.Tasks;
using Google.Apis.Services;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
using System.Threading;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Mvc;
using Google.Apis.Auth.OAuth2.Requests;
using Google.Apis.Auth.OAuth2.Web;
using Google.Apis.Json;
using Newtonsoft.Json.Linq;
using Google.Apis.Auth.OAuth2.Responses;
using System.Diagnostics;
using System.Linq;
using System.Configuration;

namespace CloudStorageLight.Core.Tests
{
    [TestClass]
    public class GoogleDriveBlobAdapterTest
    {
        public static string ClientId = ConfigurationManager.AppSettings["GoogleDriveClientId"];
        public static string ClientSecret = ConfigurationManager.AppSettings["GoogleDriveClientSecret"];
        public static string RefreshToken = ConfigurationManager.AppSettings["GoogleDriveRefreshToken"];


        [TestMethod]
        public void TestMethod1()
        {
            var data = new DictionaryDataStore();
            data.Data["/GOOGLE/"] = new TokenResponse() {
                RefreshToken = RefreshToken,
                TokenType = "Bearer",
                Issued = DateTime.MinValue                                 
            }; 
            var flow = new AppGoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId = ClientId,
                    ClientSecret = ClientSecret,

                },
                Scopes = new[] { DriveService.Scope.Drive },
                DataStore = data
            });

            var auth = new AppAuthorizationCodeWebApp(
                "/GOOGLE/",
                new AppFlowMetadata("/GOOGLE/", ClientId, ClientSecret, null, flow),
                null, null);
            var result = auth.AuthorizeAsync(CancellationToken.None).Result;

            var service = new DriveService(new BaseClientService.Initializer
            {
                HttpClientInitializer = result.Credential,
                ApplicationName = "ASP.NET MVC Sample"
            });

            // YOUR CODE SHOULD BE HERE..
            // SAMPLE CODE:
            var list = service.Files.List();
//            list.Q = "'root'" + " in parents and trashed=false and mimeType!='application/vnd.google-apps.folder'";
            list.Q = "'root'" + " in parents and trashed=false";
            var listResult = list.ExecuteAsync().Result;
            foreach(var item in listResult.Items.Where(x=>x.MimeType == "application/vnd.google-apps.folder"))
            {
                Debug.WriteLine(item.Title);
            }
            foreach (var item in listResult.Items.Where(x => x.MimeType != "application/vnd.google-apps.folder"))
            {
                Debug.WriteLine(item.Title);
            }
        }


        [TestMethod]
        public void ConvertFtpPathToUrlPathTest()
        {
            using (var adp = new GoogleDriveBlobAdapter("/Google/", ClientId, ClientSecret, RefreshToken, "/Workshop/", null))
            {
                var value = adp.ConvertGooglePathToUrlPath("/Workshop/Sub1/A.txt");

                Assert.AreEqual("/Google/Sub1/A.txt", value);

            }
        }

        [TestMethod]
        public void ConvertUrlPathToFtpPathTest()
        {
            using (var adp = new GoogleDriveBlobAdapter("/Google/", ClientId, ClientSecret, RefreshToken, "/Workshop/", null))
            {
                var value = adp.ConvertUrlPathToGooglePath("/Google/Sub1/A.txt");

                Assert.AreEqual("/Workshop/Sub1/A.txt", value);

            }
        }

        [TestMethod]
        public void UploadAsyncAndDeleteAsyncCopyAsyncTest()
        {
            using (var adp = new GoogleDriveBlobAdapter("/Google/", ClientId, ClientSecret, RefreshToken, "/Workshop/", null))
            {
                var temp = Path.GetTempFileName();
                try
                {
                    var itemA = new BlobFile(null, "/Google/A.txt");
                    var itemB = new BlobFile(null, "/Google/Sub1/Sub2/B.txt");
                    var itemBDire = new BlobDirectory(null, "/Google/Sub1/");
                    var itemB2Dire = new BlobDirectory(null, "/Google/Sub1/Sub2/");
                    adp.DeleteAsync(itemA).Wait();
                    adp.DeleteAsync(itemB).Wait();
                    adp.DeleteAsync(itemB2Dire).Wait();
                    adp.DeleteAsync(itemBDire).Wait();

                    var value = adp.ExistsAsync("/Google/A.txt").Result;
                    Assert.AreEqual(false, value);

                    adp.UploadAsync(itemA, temp).Wait();
                    value = adp.ExistsAsync("/Google/A.txt").Result;
                    Assert.AreEqual(true, value);

                    adp.CopyAsync("/Google/Sub1/Sub2/B.txt", itemA).Wait();
                    value = adp.ExistsAsync("/Google/Sub1/Sub2/B.txt").Result;
                    Assert.AreEqual(true, value);

                }
                finally
                {
                    System.IO.File.Delete(temp);
                }

            }
        }

        [TestMethod]
        public void GetFilesAsyncTest()
        {
            using (var adp = new GoogleDriveBlobAdapter("/Google/", ClientId, ClientSecret, RefreshToken, "/Workshop/", null))
            {
                var list = adp.GetFilesAsync("/Google/", 0, false).Result;
                Assert.IsTrue(list.Count > 0);

                var itemBDire = new BlobDirectory(null, "/Google/Sub1/Sub2/");
                adp.CreateDirectoryAsync(itemBDire).Wait();
                var list2 = adp.GetFilesAsync("/Google/Sub1/", 0, true).Result;
                Assert.IsTrue(list2.Count > 0);
            }

        }


    }


}
